﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DBUtility; 

namespace SQLServerDAL
{
    public class ProductBrandBase
    {
        public ProductBrandBase()
        { 
        
        }

        public string Add(Model.ProductBrandBase __news)
        {
            string result = "";

            string sqlstring = "Insert Into [ProductBrandBase] ([BrandName],[Logo],[LineNum],[Description])";
            sqlstring += "Values(@BrandName, @Logo, @LineNum, @Description)";
            SqlParameter[] prams = {
				SqlHelper.CreateInputParam("@BrandName", SqlDbType.NVarChar, __news.BrandName),
				SqlHelper.CreateInputParam("@Logo", SqlDbType.VarChar, __news.Logo), 
                SqlHelper.CreateInputParam("@LineNum",SqlDbType.Int, __news.LineNum),
                SqlHelper.CreateInputParam("@Description",SqlDbType.Text, __news.Description)
			};

            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlstring, prams);
                result = "succeeded";
            }
            catch (Exception ex)
            {
                result = ex.ToString();
            }

            return result;
        }

        public string Update(Model.ProductBrandBase __news)
        {
            string result = "";

            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
            conn.Open();
            using (SqlTransaction trans = conn.BeginTransaction())
            {
                try
                {
                    string sqlstring = "UPDATE [ProductBrandBase] SET [BrandName] = @BrandName,[Logo] = @Logo,[LineNum] = @LineNum,[Description] = @Description Where BrandId = @BrandId";
                    SqlParameter[] prams = {
						SqlHelper.CreateInputParam("@BrandId", SqlDbType.Int, __news.BrandId),
				        SqlHelper.CreateInputParam("@BrandName", SqlDbType.NVarChar, __news.BrandName),
				        SqlHelper.CreateInputParam("@Logo", SqlDbType.VarChar, __news.Logo), 
                        SqlHelper.CreateInputParam("@LineNum",SqlDbType.Int, __news.LineNum),
                        SqlHelper.CreateInputParam("@Description",SqlDbType.Text, __news.Description)
					};
 
                    SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqlstring, prams);

                    trans.Commit();
                    result = "succeeded";
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                    trans.Rollback();
                    throw ex;
                }
            }
            conn.Close();

            return result;
        }

        public Model.ProductBrandBase GetObjById(int brandid)
        {
            Model.ProductBrandBase __obj = null;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ProductBrandBase WHERE [BrandId] = @BrandId;");

            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@BrandId", SqlDbType.Int) };
            parameters[0].Value = brandid;

            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString(), parameters);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                __obj = new Model.ProductBrandBase();
                __obj.BrandId = int.Parse(dr["BrandId"].ToString());
                __obj.BrandName = dr["BrandName"].ToString();
                __obj.Logo = dr["Logo"].ToString();
                __obj.LineNum = int.Parse(dr["LineNum"].ToString());
                __obj.Description = dr["Description"].ToString();

                dt.Dispose();
            }

            return __obj;
        }

        public void Delete(int brandid)
        {
            StringBuilder strSql = new StringBuilder();
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@BrandId", SqlDbType.Int) };
            parameters[0].Value = brandid;

            strSql.Append("DELETE FROM ProductBrandBase WHERE [BrandId] = @BrandId;");

            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString(), parameters);
        }
    }
}
